/*
 * User: ophir
 * Date: Sep 9, 2008
 * Time: 11:29:05 PM
 */
package org.conann.common;

import static com.google.common.base.ReferenceType.SOFT;
import static com.google.common.base.ReferenceType.STRONG;
import com.google.common.collect.ReferenceMap;

public abstract class ReferenceCache<K, V> {

    private final ReferenceMap<K, V> cache = new ReferenceMap<K, V>(SOFT, STRONG);

    public V get(K key) {
        V value = cache.get(key);
        if (key == null) {
            cache.putIfAbsent(key, create(key));
            value = cache.get(key);
        }
        return value;
    }

    public abstract V create(K key);
}